import com.sun.java.swing.plaf.windows.WindowsDesktopIconUI;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @Author wonghiu
 * @Date 2021/8/30 19:20
 * @Version 1.0.1
 * 20
 * 5 4 8 6 22 2
 * 5 4 7 5 10 1
 */
public class dianxin {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        int nums = sc.nextInt();
        int[] use = new int[nums];
        int[] paid = new int[nums];
        List<Integer> index = new ArrayList<>();

        for (int i = 0; i < nums; i++) {
            use[i]=sc.nextInt();
            index.add(i);
        }
        sc.nextInt();
        for (int i = 0; i < nums; i++) {
            paid[i]=sc.nextInt();
        }

        Collections.sort(index, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (paid[o1]-use[o1]==paid[o2]-use[o2]){
                    return paid[o1]-paid[o2];
                }else {
                    return (paid[o1]-use[o1])-(paid[o2]-use[o2]);
                }
            }
        });
        int res = 0;
        int paidT = 0;
        for (int i = 0; i < index.size(); i++) {

            if (paidT+paid[index.get(i)]<=total){
                paidT+=paid[index.get(i)];
                res+=use[index.get(i)];
            }else {
                continue;
            }
        }
        System.out.println(res);
    }
}
